#### SENSITIVITY TEST: Omit candidates with strange names ####
ced_drop <- ced %>% 
  filter(lname != "La",
         lname != "Le",
         lname != "Mc",
         lname != "De",
         lname != "The",
         lname != "St",
         lchars != 1) %>% 
  filter(fname != "La",
         fname != "Le",
         fname != "Mc",
         fname != "De",
         fname != "The",
         fname != "St",
         fchars != 1)

nrow(ced) - nrow(ced_drop)
# Less than 1 percent of the sample of names

primary_drop <- primary %>% 
  filter(lname != "La",
         lname != "Le",
         lname != "Mc",
         lname != "De",
         lname != "The",
         lname != "St",
         lchars != 1) %>% 
  filter(fname != "La",
         fname != "Le",
         fname != "Mc",
         fname != "De",
         fname != "The",
         fname != "St",
         fchars != 1)

nrow(primary) - nrow(primary_drop)
# Less than 1 percent 

general_drop <- general %>% 
  filter(lname != "La",
         lname != "Le",
         lname != "Mc",
         lname != "De",
         lname != "The",
         lname != "St",
         lchars != 1) %>% 
  filter(fname != "La",
         fname != "Le",
         fname != "Mc",
         fname != "De",
         fname != "The",
         fname != "St",
         fchars != 1)

nrow(general) - nrow(general_drop)
# Less than 1 percent 

### This is what was omitted
# La
# Le
# Mc
# De
# The
# St
# Single character in last name
# Single character in first name

#### Run models ####
# CA local elections
mod_all_ced <- lm(form_ced_main,
                  data = ced_drop)
cedr2 <- get_r2(mod_all_ced)
mod_all_ced <- get_clusters(mod_all_ced)

# Primary congressional elections
mod_all_primary <- lm(form_primary_main,
                      data = primary_drop)

primaryr2 <- get_r2(mod_all_primary)
mod_all_primary <- get_clusters(mod_all_primary)

# General congressional elections
mod_all_general <- lm(form_general_main,
                      data = general_drop)

generalr2 <- get_r2(mod_all_general)
mod_all_general <- get_clusters(mod_all_general)


stargazer(mod_all_general, mod_all_primary, mod_all_ced,
          type = "latex",
          title = "Relationship Between Name Fluency and Vote Share",
          style = style,
          star.cutoffs = star.cutoffs,
          star.char = star.char,
          column.labels = column.labels,
          keep.stat = keep.stat,
          notes = notes,
          keep = keep,
          covariate.labels = covariate.labels,
          font.size = "footnotesize",
          model.names = F,
          add.lines=list(c("Year FE", "\\checkmark", "\\checkmark", "\\checkmark"),
                         c("State FE", "\\checkmark", "\\checkmark", ""),
                         c("County FE", "", "", "\\checkmark"),
                         c("N", get_n(mod_all_general), get_n(mod_all_primary), get_n(mod_all_ced)),
                         c("Adj. R-squared", generalr2, primaryr2, cedr2)),
          out = "tables/reg-drop-names.tex",
          label = "tab:reg-drop-names")






#### EXTRA ####
mod_ced_drop <- lm(percent ~
                     scale(last_algorithm) +
                     scale(first_algorithm) +
                     scale(percent_freq) +
                     scale(fprop) +
                     white +
                     incumbent +
                     female +
                     lchars +
                     totvotes1000 +
                     office +
                     seats_comps +
                     factor(year) +
                     factor(co_name),
                   data = ced_drop)

mod_ced_dropr2 = round(summary(mod_ced_drop)$adj.r.squared, 4)
mod_ced_dropn = mod_ced_drop$df.residual

mod_cluster <- vcovHC(mod_ced_drop, type = "HC0", cluster = "raceid", adjust = T)
mod_ced_drop <- coeftest(mod_ced_drop, vcov = mod_cluster, save = T)

mod_primary_drop <- lm(ppct ~
                         scale(last_algorithm) +
                         scale(first_algorithm) +
                         scale(percent_freq) +
                         scale(fprop) +
                         white +
                         incumbent +
                         female +
                         lchars +
                         race +
                         num_prim_opps +
                         factor(party) +
                         factor(year) +
                         factor(state),
                       data = primary_drop)

mod_primary_dropr2 = round(summary(mod_primary_drop)$adj.r.squared, 4)
mod_primary_dropn = mod_primary_drop$df.residual

mod_cluster <- vcovHC(mod_primary_drop, type = "HC0", cluster = "dcp", adjust = T)
mod_primary_drop <- coeftest(mod_primary_drop, vcov = mod_cluster)

mod_general_drop <- lm(gpct ~
                         scale(last_algorithm) +
                         scale(first_algorithm) +
                         scale(percent_freq) +
                         scale(fprop) +
                         white +
                         incumbent +
                         female +
                         lchars +
                         race +
                         factor(party) +
                         factor(year) +
                         factor(state),
                       data = general_drop)

mod_general_dropr2 = round(summary(mod_general_drop)$adj.r.squared, 4)
mod_general_dropn = mod_general_drop$df.residual

mod_cluster <- vcovHC(mod_general_drop, type = "HC0", cluster = "id", adjust = T)
mod_general_drop <- coeftest(mod_general_drop, vcov = mod_cluster) 


stargazer(mod_general_drop, mod_primary_drop, mod_ced_drop,
          type = "latex",
          title = "Relationship Between Name Fluency and Vote Share Excluding Candidates with Parial First and Last Names",
          style = "ajps",
          ci = F,
          model.names = F,
          star.cutoffs = c(.05, .01, .001),
          star.char = c("*", "**", "***"),
          notes = "Standard errors are clustered by election",
          keep = c("last_algorithm",
                   "first_algorithm",
                   "percent_freq",
                   "fprop"),
          covariate.labels = c("Surname Surname Pronounceability",
                               "First Name Surname Pronounceability",
                               "Last Name Commonality",
                               "First Name Commonality"),
          add.lines=list(c("Year FE", "\\checkmark", "\\checkmark", "\\checkmark"),
                         c("State FE", "X", "\\checkmark", "\\checkmark"),
                         c("County FE", "\\checkmark", "X", "X"),
                         c("Controls", "\\checkmark", "\\checkmark", "\\checkmark"),
                         c("N", mod_general_dropn, mod_primary_dropn, mod_ced_dropn),
                         c("Adj. R-squared", mod_general_dropr2, mod_primary_dropr2, mod_ced_dropr2)),
          align = F,
          column.labels = c("General Elections", "Primary Elections", "Local Elections"),
          keep.stat = c("n", "adj.rsq"),
          out = "Tables/reg-drop-partial-names.tex",
          label = "tab:reg-drop-partial-names")

